// vite.config.js
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { viteMockServe } from 'vite-plugin-mock'

export default defineConfig(({ mode }) => {
  // 读取对应环境变量
  const env = loadEnv(mode, process.cwd())
  return {
    server: {
      host: '127.0.0.1',
      historyApiFallback: true,
      proxy: env.VITE_ENV === 'mock'
        ? undefined
        : {
            // 只在非mock环境下配置代理
            '/api': {
              target: env.VITE_API_BASE,
              changeOrigin: true,
              rewrite: path => path.replace(/^\/api/, '')
            }
          }
    },
    plugins: [
      react(),
      viteMockServe({
        mockPath: './mock',
        localEnabled: env.VITE_ENV === 'mock',
        prodEnabled: false,
        logger: true,
        supportTs: false
      })
    ],
    build: {
      outDir: 'dist',
      sourcemap: true,
      rollupOptions: {
        output: {
          manualChunks: {
            antd: ['antd'],
            react: ['react', 'react-dom'],
            axios: ['axios'],
            lunarJs: ['lunar-javascript']
          }
        }
      }
    }
  }
});